from tkinter import ttk


class StyleParser:
    def __init__(self):
        self.map_styles = {}
        self.conf_styles = {}
        self.current_target = ''
        self.current_state = ''
        pass

    def switch(self, target: str):
        if ':' in target:
            k, v = target.split(':')
            self.current_target = k.strip()
            self.current_state = v.strip()
        else:
            self.current_target = target.strip()
            self.current_state = ''

    def add_style(self, style: str, value: str):
        if self.current_state:
            if self.current_target not in self.map_styles:
                self.map_styles[self.current_target] = {}
            if style not in self.map_styles[self.current_target]:
                self.map_styles[self.current_target][style] = {}
            self.map_styles[self.current_target][style][self.current_state] = value
        else:
            if self.current_target not in self.conf_styles:
                self.conf_styles[self.current_target] = {}
            self.conf_styles[self.current_target][style] = value

    def apply(self, style: 'ttk.Style'):
        for k, v in self.conf_styles.items():
            style.configure(k, **v)
        for k, v in self.map_styles.items():
            kv = {}
            for kk, vv in v.items():
                kv[kk] = [(i, j) for i, j in vv.items()]
            style.map(k, **kv)
